//
//  NewsTableViewController+LoadData.m
//  JK九酷云头条
//
//  Created by jiukuyinyue on 2017/11/15.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "NewsTableViewController+LoadData.h"
#import "NSDictionary+Data.h"

@implementation NewsTableViewController (LoadData)

- (void)loadData:(NSString *)url page:(BOOL)page{
    if (page == NO) {
        self.themaxBloodSugar = 0; // 最大值
        self.theminBloodSugar = 0; // 最小值
     }
    NSString * list = [NSString stringWithFormat:@"%lld",self.themaxBloodSugar];
    NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:url,@"cid",list,@"last_id",[NSString stringWithFormat:@"%lld",self.theminBloodSugar],@"first_id", nil];
     NSString *str = [NSString stringWithFormat:@"http://toutiao.api.inping.com/news/lists"];
    [LSZRequestTotal initWithPostRequest:str :dic :^(BOOL isOK, NSString * str, NSDictionary * dict) {
//        NSLog(@"%@",dict);
        if (isOK == YES) {
            NSArray *dictArr = dict[@"data"];
            NSMutableArray * arrayMut = [[NSMutableArray alloc]init];
            for (NSDictionary * dic in dictArr) {
//                NSString * id_id = [NSString stringWithFormat:@"%@",dic[@"id"]];
                if (![[NSString stringWithFormat:@"%@",dic[@"id"]] isEqualToString:@"0"]) {
                    [arrayMut addObject:dic];
                    long long id_id = [dic[@"id"] longLongValue];
                    if (self.themaxBloodSugar == 0 && self.themaxBloodSugar == 0) {
                        self.themaxBloodSugar = id_id;//只要获取一跳数据就赋值最大和最小
                        self.theminBloodSugar = id_id;//
                    }else{
                        if (id_id > self.themaxBloodSugar) {
                            self.themaxBloodSugar = id_id;//比最大值大
                        }
                        if (id_id <self.theminBloodSugar) {
                            self.theminBloodSugar = id_id;//比最小值小
                        }
                    }
                }
            }
//            NSLog(@"self.themaxBloodSugar = %llu", self.themaxBloodSugar);
//            NSLog(@"self.theminBloodSugar = %llu", self.theminBloodSugar);
            //YYModel实现字典数组转模型数组
            NSArray<NewsModel *> *dictArr2 = [NSArray yy_modelArrayWithClass:[NewsModel class] json:arrayMut];
            if(dictArr2.count > 0){
                dictArr2[0].cid = self.cid;//无意义 只是用来做判断是否是当前栏目cid.所以赋值第0个的栏目cid 为self.cid
            }
            dispatch_async(dispatch_get_global_queue(0, 0), ^{//由于会卡顿 所以在分线程里检测网络,主线程刷新显示无网络
                NSLog(@"22222");
            });
            if (page) {
                if (dictArr2.count == 0) {
                    [self NoDataDisappeared];
                    return ;
                }
                [self.dataSource addObjectsFromArray:dictArr2];
            }else{
                [self setupPageOneData:dictArr2];
            }
            // 拿到数据源数组之后,回到主线程刷新UI
            [self refreshTableView];
        }else{
            NSLog(@"1111111");
        }
    }];
}
#pragma mark - 没有数据也消失
-(void)NoDataDisappeared
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
         [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}
///添加 数据 后 刷新界面显示数据
- (void)refreshTableView {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}
///检测网络
- (void)netError {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{//由于会卡顿 所以在分线程里检测网络,主线程刷新显示无网络
        Reachability *reach = [Reachability reachabilityWithHostName:@"http://baidu.com"];
        if ([reach currentReachabilityStatus] == NotReachable){
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SVProgressHUD showErrorWithStatus:NoNetwork];
                [SVProgressHUD dismissWithDelay:0.5];
            }];
        }
    });
}
//将首页的前10条数据赋值到 dataSource 属性里
- (void)setupPageOneData:(NSArray<NewsModel *> *)dictArr2 {
    self.dataSource = [NSMutableArray arrayWithArray:dictArr2];
}
@end
